local jingu = fk.CreateSkill{
  name = "xiaobai__jingu",
  tags = {Skill.Compulsory}
}

Fk:loadTranslationTable{
  xiaobai__jingu = "金谷",
  [":xiaobai__jingu"] = "锁定技，结束阶段，你装备区内每有一种花色，便获得本回合弃牌堆中一张【杀】或【闪】，若不足，则不足部分用【影】补齐。"
}

local U = require "packages.utility.utility"

jingu:addEffect(fk.EventPhaseStart, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and target == player and player.phase == Player.Finish
  end,
  on_use = function (self, event, target, player, data)
    local suits = {}
    local room = player.room
    table.forEach(player:getCardIds("ej"), function(id) table.insertIfNeed(suits, Fk:getCardById(id).suit) end)
    local ids = table.simpleClone(player:getTableMark("xiaobai__jingu-turn"))
    if #ids >= #suits then
      table.shuffle(ids)
      ids = table.slice(ids, 1, #suits + 1)
      room:obtainCard(player, ids, true, fk.ReasonPrey, player, jingu.name)
    else
      room:obtainCard(player, ids, true, fk.ReasonPrey, player, jingu.name)
      local shades = U.getShade(room, #suits - #ids)
      room:obtainCard(player, shades, true, fk.ReasonPrey, player, jingu.name)
    end
  end
})

jingu:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(jingu.name, true) then
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(player.room.discard_pile, info.cardId) then
              return true
            end
          end
        end
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.DiscardPile and not table.contains(player.room.discard_pile, info.cardId) then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local ids = player:getTableMark("xiaobai__jingu-turn")
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if table.contains(room.discard_pile, info.cardId) and (Fk:getCardById(info.cardId).trueName == "slash" or 
            Fk:getCardById(info.cardId).trueName == "jink") then
            table.insertIfNeed(ids, info.cardId)
          end
        end
      end
      for _, info in ipairs(move.moveInfo) do
        if info.fromArea == Card.DiscardPile and not table.contains(room.discard_pile, info.cardId) then
          table.removeOne(ids, info.cardId)
        end
      end
    end
    room:setPlayerMark(player, "xiaobai__jingu-turn", ids)
  end,
})

jingu:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  local ids = {}
  room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
    for _, move in ipairs(e.data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if table.contains(room.discard_pile, info.cardId) and (Fk:getCardById(info.cardId).trueName == "slash" or 
            Fk:getCardById(info.cardId).trueName == "jink") then
            table.insertIfNeed(ids, info.cardId)
          end
        end
      end
    end
  end, Player.HistoryTurn)
  room:setPlayerMark(player, "xiaobai__jingu-turn", ids)
end)

return jingu